package org.weso.acota.appserv;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;

import javax.xml.transform.TransformerException;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.NodeIterator;
import org.weso.acota.exceptions.DocumentBuilderException;
import org.weso.acota.to.RequestSuggestionTO;
import org.weso.acota.to.ResourceTO;
import org.weso.acota.to.SuggestionTO;
import org.weso.acota.utils.DocumentBuilderHelper;

public class SuggestAppServEvaluation {

	@SuppressWarnings("rawtypes")
	@Test
    public void suggestEvaluation() throws FileNotFoundException, DocumentBuilderException, TransformerException {
		long tiempoInicio = System.currentTimeMillis();
		InputStream doc = getClass().getClassLoader().getResourceAsStream("org/weso/acota/loader/news.xml"); 
		Document newsXML = DocumentBuilderHelper.getDocumentFromInputStream(doc);
		 NodeIterator it;
		 //Extract the id of new
		 it = org.apache.xpath.XPathAPI.selectNodeIterator(newsXML, "//@id");
		 Node n = it.nextNode();
		 int appearsGeneral=0;
		 int total=0;
		 int totalAnalyzed=0;
         while(n != null){
        	 if(total>=400){
        		 
             String descripction;
        	 String label;
        	 String url;
        	 String tags;
        	 String[] tagsList;
         	//Extract tittle of each new and save in a label variable
         	NodeIterator titleNode = org.apache.xpath.XPathAPI.selectNodeIterator(newsXML, "//new[@id='"+n.getNodeValue()+"']/title/text()");
         	try {
         		label = titleNode.nextNode().getNodeValue();
			} catch (Exception e) {
				label ="";
			}
         	
         	//Extract description of each new and save in a description variable
         	NodeIterator descriptionNode = org.apache.xpath.XPathAPI.selectNodeIterator(newsXML, "//new[@id='"+n.getNodeValue()+"']/comment/text()");
         	try {
         		descripction = descriptionNode.nextNode().getNodeValue();
			} catch (Exception e) {
				descripction ="";
			}
			
			//Extract url of each new and save in a description variable
         	NodeIterator urlNode = org.apache.xpath.XPathAPI.selectNodeIterator(newsXML, "//new[@id='"+n.getNodeValue()+"']/url/text()");
         	try {
         		url = urlNode.nextNode().getNodeValue();
			} catch (Exception e) {
				url ="";
			}
			
			//Extract tag's of each new and save in a description variable
         	NodeIterator tagNode = org.apache.xpath.XPathAPI.selectNodeIterator(newsXML, "//new[@id='"+n.getNodeValue()+"']/keywords/text()");
         	try {
         		tags = tagNode.nextNode().getNodeValue();
			} catch (Exception e) {
				tags ="";
			}
			tagsList = tags.split(",");
			
			//Create the resource object
			ResourceTO resource = new ResourceTO();
			resource.setLabel(label);
			resource.setDescription(descripction);
			resource.setUri(url);
			
			//Create the request
			RequestSuggestionTO request = new RequestSuggestionTO(resource);
			
			//Send the request
			SuggestAppServ appServ = new SuggestAppServ();
			SuggestionTO suggest = appServ.suggest(request);
			
			
			
			//Print results
         	System.out.println("Resourse -->"+n.getNodeValue()+" Ready");
         	Iterator itr = suggest .getLabels().entrySet().iterator();
			while (itr.hasNext()) {
				Map.Entry e = (Map.Entry)itr.next();
				System.out.println(""+e.getKey()+";"+e.getValue()+"");
			}
         	
         	//tags is or not?
			int appear=0;
			//System.out.println("Own tags->\n");
			for(int f=0; f < tagsList.length ; f++){
				//System.out.println(tagsList[f].trim().toLowerCase()+"\n");
				if (suggest.getLabels().containsKey(tagsList[f].trim().toLowerCase()))
					appear++;
			}
			if(appear>=1)
         		appearsGeneral++;
         	
         	//System.out.println("Appears->"+appear+"\n");
         	totalAnalyzed++;
         	}
         	total++;
         	n = it.nextNode();
         }
         long totalTiempo = System.currentTimeMillis() - tiempoInicio;
         System.out.println("\n*****Totals********\n");
         System.out.println("Total of resourses:"+totalAnalyzed+"\n");
         System.out.println("Total appears:"+appearsGeneral+"\n");
         System.out.println("Runtime :" + totalTiempo + " miliseg");
    }
}     

